$(function () {
    $('#save-market-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#market-dialog');
        var name = $("input[name='name']").val();
        var market_img = $("input[name='market_img']").val();
        var price = $("input[name='price']").val();
        var number = $("input[name='number']").val();
        var rise = $("input[name='rise']").val();
        var priority = $("input[name='priority']").val();


        // var type_id = document.getElementsByName("type_id")[0].value;
        var submitType = self.attr("data-type");
        var market_id = self.attr('data-id');

        var url = '';
        if (submitType == 'update'){
            url = '/cms/update_market/';
        }else{
            url = '/cms/add_market/';
        }

        zlajax.post({
            'url':url,
            'data':{
                'name':name,
                'market_img':market_img,
                'price':price,
                'number':number,
                'priority':priority,
                'rise':rise,
                'market_id':market_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    zlalert.alertInfo(data['message']);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".edit-market-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#market-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var name = tr.attr("data-name");
        var price = tr.attr("data-price");
        var market_img = tr.attr("data-img");
        var priority = tr.attr("data-priority");
        var number = tr.attr("data-number");
        var rise = tr.attr("data-rise");

        var nameInput = dialog.find("input[name='name']");
        var imageInput = dialog.find("input[name='market_img']");
        var priceInput = dialog.find("input[name='price']");
        var priorityInput = dialog.find("input[name='priority']");
        var numberInput = dialog.find("input[name='number']");
        var riseInput = dialog.find("input[name='rise']");
        var savebtn = dialog.find("#save-market-btn");

        nameInput.val(name);
        imageInput.val(market_img);
        priceInput.val(price);
        priorityInput.val(priority);
        numberInput.val(number);
        riseInput.val(rise);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.delete-market-btn').click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var market_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这条记录吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/del_market/',
                    'data':{
                        'market_id':market_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            zlalert.alertError(data['message'])
                        }
                    }
                })
            }
        })
    })
});



$(function () {
    $('#market_info').click(function (event) {
        var url = 'http://127.0.0.1:5000/task/update_market/';
        zlajax.get({
            'url':url,
            'data':{},
            'success':function (data) {
                if(data['code']==200){
                    window.location.reload()
                }else{
                    zlalert.alertInfo(data['message']);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});